package se.webbzon.oschi01.worldcamera;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.media.j3d.Canvas3D;

import se.webbzon.oschi01.friendly.Friendly;
import se.webbzon.oschi01.worldarea.WorldAppearance3D;

import com.sun.j3d.exp.swing.JCanvas3D;
import com.sun.j3d.utils.universe.SimpleUniverse;

@Friendly class SimpleJWorldView extends AbstractJWorldView implements SimpleWorldView {
	
	// Auto generated serial version UID
	private static final long serialVersionUID = 4042431742765546042L;

	// The simple universe of this view
	private final SimpleUniverse u;
	
	// The camera components of this world view
	private final WorldCamera camera;
	private final WorldCameraHud hud;
	private final JWorldCameraBackground background;
    
    // The world view symbiosis of this world view
    private WorldViewSymbiosis symb;

	/*============================================================
	Constructors
	============================================================*/

    @Friendly SimpleJWorldView(int viewProperties, Dimension d) {
    	setLayout(new BorderLayout());
		// Create the 3d canvas
		JCanvas3D jCanvas3d = new ExtendedJCanvas3D(d);
		setPreferredSize(d);
		add(jCanvas3d,BorderLayout.CENTER);
		Canvas3D canvas3d = jCanvas3d.getOffscreenCanvas3D();
		
		u = new SimpleUniverse(canvas3d);
		
		if (isSet(viewProperties,TRANSPARENCY))
            camera = new SimpleWorldCamera(WorldCamera.TRANSPARENCY, u.getViewingPlatform());
		else
            camera = new SimpleWorldCamera(0, u.getViewingPlatform());
		hud = new WorldCameraHud(canvas3d,d);
		background = new JWorldCameraBackground(canvas3d,d);
		
		u.addBranchGraph(background.getJ3dBranch());
	}
	
	/*============================================================
	Constructors
	============================================================*/
	
    @Override public SimpleUniverse getUniverse() {
		return u;
	}

	@Override public void preRender(WorldAppearance3D world) {
		background.repaint(world);
	}

	@Override public void postRender(WorldAppearance3D world) {
		world.performPostDraw(hud);
	}

	@Override public boolean setSymbiosis(WorldViewSymbiosis symb) {
		this.symb = symb;
		return true;
	}

	@Override public WorldCamera getCamera() {
		return camera;
	}

	@Override public WorldCameraPane getCameraHud() {
		return hud;
	}

	@Override public WorldCameraPane getCameraBackground() {
		return background;
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Returns true if the given property is set in the given properties. **/
	private static final boolean isSet(int properties, int property) {
		return ((properties & property) == property);
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class ExtendedJCanvas3D extends JCanvas3D {
		
		// Auto generated serial version UID
		private static final long serialVersionUID = 7998742705066124391L;
		
		/*============================================================
		Constructors
		============================================================*/

		/** Creates an extended JCanvas3D with the provided dimension. **/
		public ExtendedJCanvas3D(Dimension d) {
			setSize(d);
			setPreferredSize(d);
		}
		
		/*============================================================
		Public Methods
		============================================================*/
	
		@Override public void paintComponent(Graphics g) {
			if (symb != null)
				symb.preRender(SimpleJWorldView.this);
			super.paintComponent(g);
			hud.clear();
			if (symb != null)
				symb.postRender(SimpleJWorldView.this);
			hud.paint(0, 0, g, this);
		} 
	
	}

}
